package admin

import (
	"blogs/models"
	"blogs/utils"
	"encoding/json"
	"github.com/beego/beego/v2/adapter"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

// Operations about object
type ArticleController struct {
	beego.Controller
}

type Name struct {
	Tag     []int
	Title   string
	Nav     int
	Content string
}

// @Title Create
// @Description 添加文章
// @Success 200 {string} models.Object.Id
// @Failure 403 body is empty
// @router / [post]
func (o *ArticleController) Post() {
	title := o.GetString("Title")
	nav := o.GetString("Nav")
	navId, _ := strconv.Atoi(nav)
	tag := o.GetStrings("Tag[]")
	contentHtml := o.GetString("ContentHtml")
	content := o.GetString("Content")
	adapter.Info(contentHtml, "html=>>><<<=html")
	article := models.Article{
		Title:       title,
		Content:     content,
		ContentHtml: contentHtml,
		Img:         "",
		AddTime:     int(utils.GetUnix()),
		NavId:       navId,
	}
	err := utils.DB.Create(&article).Error
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"msg":  "创建错误",
			"data": "创建错误",
			"code": false,
		}
		o.ServeJSON()
		return
	}

	for _, val := range tag {
		adapter.Debug(val, "?????")
		TagId, _ := strconv.Atoi(val)
		tagArticle := models.TagArticle{
			TagId:     TagId,
			ArticleID: article.Id,
		}
		utils.DB.Create(&tagArticle)
	}

	o.Data["json"] = map[string]interface{}{
		"msg":  "添加成功",
		"data": "添加成功",
		"code": true,
	}
	o.ServeJSON()

}

// @Title Get
// @Description 通过id查询文章
// @Param	objectId		path 	string	true		"the objectid you want to get"
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router /:objectId [get]
func (o *ArticleController) Get() {
	objectId := o.Ctx.Input.Param(":objectId")

	article := []models.Article{}
	utils.DB.Where("id=?", objectId).Find(&article)
	if len(article) > 0 {
		o.Data["json"] = map[string]interface{}{
			"msg":  "查询成功",
			"data": article,
			"code": true,
		}
		o.ServeJSON()
		return
	}
	o.Data["json"] = map[string]interface{}{
		"msg":  "查询失败",
		"data": "查询失败",
		"code": false,
	}
	o.ServeJSON()
}

// @Title GetAll
// @Description 查询所有文章
// @Success 200 {object} models.Article
// @Failure 403 :objectId is empty
// @router / [get]
func (o *ArticleController) GetAll() {

	article := []models.Article{}
	utils.DB.Preload("Tag").Preload("Nav").Find(&article)

	o.Data["json"] = map[string]interface{}{
		"msg":  "查询成功",
		"data": article,
		"code": false,
	}
	o.ServeJSON()
}

// @Title Update
// @Description 更新文章
// @Param	objectId		path 	string	true		"The objectid you want to update"
// @Param	body		body 	models.Article	true		"The body"
// @Success 200 {object} models.Article
// @Failure 403 :objectId is empty
// @router /:objectId [put]
func (o *ArticleController) Put() {

	// 接收参数
	objectId := o.Ctx.Input.Param(":objectId")
	var ob models.Article
	json.Unmarshal(o.Ctx.Input.RequestBody, &ob)

	// 更新数据
	article := []models.Article{}
	utils.DB.Where("id=?", objectId).Find(&article)
	if len(article) > 0 {
		article[0].AddTime = ob.AddTime
		article[0].Title = ob.Title
		article[0].Content = ob.Content
		utils.DB.Save(&article)
		o.Data["json"] = map[string]interface{}{
			"msg":  "更新成功",
			"data": "更新成功",
			"code": true,
		}
		o.ServeJSON()
		return
	}
	o.Data["json"] = map[string]interface{}{
		"msg":  "更新失败",
		"data": "更新失败",
		"code": false,
	}
	o.ServeJSON()
}

// @Title Delete
// @Description 删除文章
// @Param	Id		query 	  	string	true		"The Id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 objectId is empty
// @router / [delete]
func (o *ArticleController) Delete() {
	objectId := o.GetString("Id")

	article := []models.Article{}
	err := utils.DB.Where("id=?", objectId).Delete(&article).Error
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"msg":  "删除失败",
			"data": "删除失败",
			"code": false,
		}
		o.ServeJSON()
		return
	}
	o.Data["json"] = map[string]interface{}{
		"msg":  "删除成功",
		"data": "删除成功",
		"code": true,
	}
	o.ServeJSON()

}
